﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace ERP.Classes
{
    public class cs_hmr_menuInfo
    {
        private int _menu_id;
        private string _menu_name;
        private string _menu_task;
        private int _id_menu_cha;

        
        public cs_hmr_menuInfo()
        {
            this._menu_id = 0;
            this._menu_name = " ";
            this._menu_task = " ";
            this._id_menu_cha = 0;

        }
        public int menu_id
        {
            get
            {
                return this._menu_id;
            }
            set
            {
                this._menu_id = value;
            }
        }
        public string menu_name
        {
            get
            {
                return this._menu_name;
            }
            set
            {
                this._menu_name = value;
            }
        }
        public string menu_task
        {
            get
            {
                return this._menu_task;
            }
            set
            {
                this._menu_task = value;
            }
        }
        public int id_menu_cha
        {
            get
            {
                return this._id_menu_cha;
            }
            set
            {
                this._id_menu_cha = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[5];
            paraName[0] = "@menu_id";
            paraName[1] = "@menu_name";
            paraName[2] = "@menu_task";
            paraName[3] = "@id_menu_cha";
            paraName[4] = "@option";
            object[] paraValue = new object[5];
            paraValue[0] = this._menu_id;
            paraValue[1] = this._menu_name;
            paraValue[2] = this._menu_task;
            paraValue[3] = this._id_menu_cha;
            paraValue[4] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 5, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        public void KetDulieuVaoListview(ListView myList)
        {
            csListView list = new csListView();
            string[] fields = new string[4];
            fields[0] = "menu_id";
            fields[1] = "menu_name";
            fields[2] = "menu_task";
            fields[3] = "id_menu_cha";
            list.KetDulieu(myList, "HMR_Menu", "", fields);
        }
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_HMR_Menu", option);
        }

        public void ShowDataInCombo(ComboBox cbo, string condition)
        {
            csComboBox cb = new csComboBox("menu_name", "menu_id");
            cb.KetDuLieu(cbo, "HMR_Menu", condition);
        }

        public int ShowDataGrid(DataGrid datagrid, string condition)
        {
            csDataGrid grid = new csDataGrid();

            string[] header = new string[4];
            header[0] = "menu_id";
            header[1] = "menu_name";
            header[2] = "menu_task";
            header[3] = "id_menu_cha";

            string[] mappingname = new string[4];
            mappingname[0] = "menu_id";
            mappingname[1] = "menu_name";
            mappingname[2] = "menu_task";
            mappingname[3] = "id_menu_cha";

            int[] colwidth = new int[4];
            colwidth[0] = 12;
            colwidth[1] = 300;
            colwidth[2] = 1200;
            colwidth[3] = 12;
            csCSDL csdl = new csCSDL();
            return grid.CreateTableStyle("HMR_Menu", datagrid, csdl.OpenTable("HMR_Menu", condition), header, mappingname, colwidth, 4);
        }
        public int Get_Info(int option)
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                if (option == 1)
                {
                    ds = csdl.OpenTable("HMR_Menu", "menu_id = " + this._menu_id);
                }
                else
                {
                    ds = csdl.OpenTable("HMR_Menu", "menu_name = '" + this._menu_name + "'");
                }
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._menu_id = Convert.ToInt32(ds.Tables[0].Rows[0]["menu_id"].ToString().Trim());
                this._menu_name = ds.Tables[0].Rows[0]["menu_name"].ToString().Trim();
                this._menu_task = ds.Tables[0].Rows[0]["menu_task"].ToString().Trim();
                this._id_menu_cha = Convert.ToInt32(ds.Tables[0].Rows[0]["id_menu_cha"].ToString().Trim());
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        public DataSet Get_List()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.menu_id, rtrim(a.menu_name) as menu_name, rtrim(b.menu_name) as cha, rtrim(a.menu_task) as menu_task" 
                        + " from HMR_MENU a"
                        + " left join HMR_MENU b on (b.menu_id = a.id_menu_cha) "
                        + " where a.menu_id is not null "
                        + " order by a.id_menu_cha asc";
                ds = csdl.SelecSQL(sql);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                
                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Menu_cha()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.menu_id, rtrim(a.menu_task) as menu_task"
                        + " from HMR_MENU a"
                        + " order by a.menu_task asc";
                ds = csdl.SelecSQL(sql);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }

                return ds;
            }
            catch
            {
                return null;
            }
        }
    }
}
